package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.PastResponse;
import com.right.health.app.net.SickHistoryApi;

/*
 *   既往史
 * Created by hopeliao on 2019-10-01
 *
 */
public class PastActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {
    private static final int HTTP_CODE_UPDATE = 1;
    private static final int HTTP_CODE_GET = 2;

    private TextView mContentTv, mRightTv;

    private EditText mContentEt;

    private boolean mEditEnable = false;

    private SickHistoryApi mSickHistoryApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.past);
    }

    @Override
    public void findView() {
        mRightTv = findViewById(R.id.tv_edit);
        mRightTv.setOnClickListener(this);
        mContentTv = (TextView) findViewById(R.id.tv_content);
        mContentTv.setVisibility(View.VISIBLE);
        mContentEt = (EditText) findViewById(R.id.et_content);
        mContentEt.setVisibility(View.GONE);

        findViewById(R.id.btn_save).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);

        mSickHistoryApi.addRequestCode(HTTP_CODE_GET);
        mSickHistoryApi.getPast(App.getInstance().getCurrentMember().id);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_edit) {
            mEditEnable = !mEditEnable;

            if (mEditEnable) {
                mContentEt.setVisibility(View.VISIBLE);
                mContentTv.setVisibility(View.GONE);
                mRightTv.setText("取消");
            } else {
                mContentEt.setVisibility(View.GONE);
                mContentTv.setVisibility(View.VISIBLE);
                mRightTv.setText("编辑");
            }
        } else if (v.getId() == R.id.btn_save) {
            if (mEditEnable) {
                String content = mContentEt.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.show(this, mContentEt.getHint().toString().trim());
                    return;
                }
                mSickHistoryApi.addRequestCode(HTTP_CODE_UPDATE);
                mSickHistoryApi.setTag(content);
                mSickHistoryApi.updatePast(App.getInstance().getCurrentMember().id, content);
            }
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_UPDATE) {
                ToastUtil.show(this, "修改成功");
                String content = String.valueOf(request.tag);
                mContentTv.setText(content);
                findViewById(R.id.tv_edit).performClick();
            } else {
                if (response.targetData instanceof PastResponse) {
                    PastResponse info = response.cast(PastResponse.class);
                    if (info.pasthistory != null) {
                        mContentTv.setText(info.pasthistory.pasthistorydesc);
                    }
                }
            }
        }
    }
}
